﻿using HPIT.RentHouse.Common;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using HPIT.RentHouse.Service.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HPIT.RentHouse.Service
{
    public class PermissionsService : IPermissionsService
    {
        /// <summary>
        /// 获取权限列表
        /// </summary>
        /// <returns></returns>
        public List<PermissionDTO> GetList()
        {
            RentHouseEntities db = new RentHouseEntities();
            BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
            List<PermissionDTO> list = baseService.GetList(a => true).Select(a=>new PermissionDTO() { 
                Id=a.Id,
                Description=a.Description,
                Name=a.Name
            }).ToList();
            return list;
        }

        /// <summary>
        /// 获取分页列表数据
        /// </summary>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <param name="name"></param>
        /// <param name="count"></param>
        /// <returns></returns>

        public List<PermissionDTO> GetPageList(int start, int length, string name, ref int count)
        {
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
                //当用户没有输入权限名称的时候默认查询所有的权限信息
                //当用户输入权限名称的时候，根据输入的信息进行条件查询
                //通过lambda表达式树扩展来构造出返回所有的结果的表达式
                var query = PredicateExtensions.True<T_Permissions>();
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.And(a=>a.Name.Contains(name));
                }
                var list = baseService.GetPagedList(start, length, ref count, query, a => a.CreateDateTime, false);
                var result=list.Select(a => new PermissionDTO()
                {
                    Id = a.Id,
                    Description = a.Description,
                    Name = a.Name
                }).ToList();
                return result;
            }
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="permissionDTO"></param>
        /// <returns></returns>
        public AjaxResult AddPermission(PermissionDTO permissionDTO)
        {
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
                T_Permissions permissions = new T_Permissions()
                {
                    Name=permissionDTO.Name,
                    Description=permissionDTO.Description,
                    CreateDateTime=DateTime.Now,
                    IsDeleted=false
                };
                long id=baseService.Add(permissions);
                if (id > 0)
                {
                    return new AjaxResult(ResultState.Success, "");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "添加权限失败！");
                }
            }
        }

        /// <summary>
        /// 根据id获取权限信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
         public PermissionDTO Get(long id)
        {
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
                T_Permissions permissions= baseService.Get(a => a.Id == id);
                PermissionDTO permissionDTO = new PermissionDTO() { 
                    Id= permissions.Id,
                    Name= permissions.Name,
                    Description= permissions.Description
                };
                return permissionDTO;
            }
        }

        /// <summary>
        /// 修改权限
        /// </summary>
        /// <param name="permissionDTO"></param>
        /// <returns></returns>
        public AjaxResult EditPermission(PermissionDTO permissionDTO)
        {
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
                T_Permissions permissions = baseService.Get(a => a.Id == permissionDTO.Id);
                permissions.Name = permissionDTO.Name;
                permissions.Description = permissionDTO.Description;
                bool flag= baseService.Update(permissions);
                if (flag)
                {
                    return new AjaxResult(ResultState.Success, "");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "修改权限失败！");
                }
            }
        }

        /// <summary>
        /// 删除权限
        /// </summary>
        /// <param name="id">权限id</param>
        /// <returns></returns>
        public AjaxResult DeletePermission(long id)
        {
            using (var db = new RentHouseEntities())
            {
                BaseService<T_Permissions> baseService = new BaseService<T_Permissions>(db);
                T_Permissions permissions = baseService.Get(a => a.Id == id);
                bool flag=baseService.Delete(permissions);
                if (flag)
                {
                    return new AjaxResult(ResultState.Success, "");
                }
                else
                {
                    return new AjaxResult(ResultState.Error, "权限删除失败");
                }
            }
        }

        /// <summary>
        /// 批量删除权限
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public AjaxResult DeleteBatch(List<long> ids)
        {
            try
            {
                var db = new RentHouseEntities();
                BaseService<T_Permissions> bs = new BaseService<T_Permissions>(db);
                foreach (var id in ids)
                {
                    var model = bs.Get(e => e.Id == id);
                    bs.Delete(model);
                }
                return new AjaxResult(ResultState.Success, "删除成功");
            }
            catch (Exception)
            {
                return new AjaxResult(ResultState.Error, "删除失败");
            }
        }
    }
}
